package com.game.modular.game.act.battle;

import com.game.comment.core.ResponseResult;
import com.game.comment.util.JwtUtils;
import com.game.config.netty.annotation.ActionController;
import com.game.config.netty.annotation.ActionMethod;
import com.game.modular.game.act.battle.vo.BattleVo;
import com.game.modular.game.playerInfo.service.PlayerServiceImpl;
import com.game.modular.game.playerMatch.service.PlayerMatchServiceImpl;
import com.game.modular.game.playerMatch.domain.PvpCity;
import com.game.modular.game.battle.info.base.PvpPlayerInfo;
import com.game.modular.game.playerMatch.service.PvpCityServiceImpl;
import com.game.modular.game.battle.info.base.PvpPlayerInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "pvp城池信息")
@Slf4j
@Validated
@RestController
@AllArgsConstructor
@ActionController(1)
@RequestMapping("/pvpCityAct")
public class PvpCityAction {
    @Resource
    private PvpCityServiceImpl pvpCityServiceImpl;
    @Resource
    private PlayerServiceImpl playerService;
    @Resource
    private PvpPlayerInfoServiceImpl pvpPlayerInfoServiceImpl;
    @Resource
    private PlayerMatchServiceImpl playerMatchServiceImpl;

    @ApiOperation("所有查询城池")
    @ActionMethod(1)
    @PostMapping("/list")
    public ResponseResult<?> selectCity(
            @RequestHeader String token
    ) {
        log.info("查询所有城池");
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);
        return ResponseResult.success(pvpCityServiceImpl.listCache(currentMatchId));
    }

    @ApiOperation("自己城池")
    @ActionMethod(2)
    @PostMapping("/ownerList")
    public ResponseResult<?> listMyCity(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);

        PvpPlayerInfo pvpPlayerInfo = pvpPlayerInfoServiceImpl.get(currentMatchId, playerId);
        if (pvpPlayerInfo == null) {
            return ResponseResult.error("当前对局不存在");
        }
        if (pvpPlayerInfo.getCurrentCity().isEmpty()) {
            return ResponseResult.error("你的城池已丢完");
        }
        List<PvpCity> res = pvpPlayerInfo.getCurrentCity().stream().map(
                cityId -> pvpCityServiceImpl.getCache(currentMatchId, cityId)).collect(Collectors.toList());
        return ResponseResult.success(res);
    }

    @ApiOperation("其他玩家城池")
    @ActionMethod(3)
    @PostMapping("/listEnemyCity")
    public ResponseResult<?> listEnemyCity(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);
        List<PvpCity> list = pvpCityServiceImpl.listCache(currentMatchId);
        List<PvpCity> res = list.stream()
                .filter(city -> city.getOwnerUid()!=null // 排除空
                                &&city.getOwnerUid()!=0L // 排除中立
                        &&!city.getOwnerUid().equals(playerId)// 排除自己
                )
                .collect(Collectors.toList());
        return ResponseResult.success(res);
    }

    @ApiOperation("运输兵力")
    @ActionMethod(4)
    @PostMapping("/transport")
    public ResponseResult<?> transport(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpCityVo.transport param
    ) {
        // 获取数据
        Long playerId = JwtUtils.getPlayerId(token);
        // 返回结果
        return ResponseResult.success(pvpCityServiceImpl.transport(
                playerId, param.getStartCityId(), param.getTargetCityId(), param.getTroopsQuantity())
        );
    }
}
